package cn.edu.gsau.info.service.impl;

import cn.edu.gsau.info.models.*;
import cn.edu.gsau.info.dao.JiaogaiDao;
import cn.edu.gsau.info.myenum.ModuleIdClass;
import cn.edu.gsau.info.service.*;
import cn.edu.gsau.info.vo.Review;
import cn.edu.gsau.info.vo.ParamModel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* @BelongsProject: JiaogaiServiceImpl
* @BelongsPackage: cn.edu.gsau.info.service.impl
* @Author: kql
* @CreateTime: 2022/11/18
* @Description:
* @Version: 1.0
*/
@Service
public class JiaogaiServiceImpl extends ServiceImpl<JiaogaiDao, Jiaogai> implements IJiaogaiService {

    @Autowired
    JiaogaiDao jiaogaiDao;

    @Autowired
    IZhengmingcailiaoService zhengMingCaiLiaoService;

    @Autowired
    IUserService userService;

    @Autowired
    IReviewService reviewService;

    @Autowired
    IShenheService shenHeService;

    /**
     * @Description 通过填写人id查询教改项目
     * @Param [id]
     * @return com.baomidou.mybatisplus.core.metadata.IPage
     * @Date 2022/11/21 21:15
     * @Author kql
     **/
    @Override
    public IPage selectJiaoGai(long id,ParamModel paramModel) {
        IPage<Jiaogai> page=new Page<>(paramModel.getPageindex(),5);
        System.out.println(page.getSize());
        HashMap<String,String> map = new HashMap<>();
        map.put("key",paramModel.getKey());
        map.put("status",paramModel.getStatus());
        QueryWrapper qw = new QueryWrapper();
        qw.like(map.get("key")!=null && !map.get("key").equals(""),"projectName",map.get("key"));
        qw.eq(map.get("status")!=null && !map.get("status").equals(""),"status",map.get("status"));
        qw.eq("tianbaoId",id);
        IPage iPage = jiaogaiDao.selectPage(page, qw);
        return iPage;
    }
    /**
     * @Description //TODO
     * @Param [paramModel]
     * @return com.baomidou.mybatisplus.core.metadata.IPage
     * @Date 2022/11/28 19:53
     * @Author kql
     **/
    @Override
    public IPage shenPiJiaoGai(ParamModel paramModel) {
        IPage<Jiaogai> page=new Page<>(paramModel.getPageindex(),5);
        System.out.println(page.getSize());
        HashMap<String,String> map = new HashMap<>();
        map.put("key",paramModel.getKey());
        map.put("status",paramModel.getStatus());
        QueryWrapper qw = new QueryWrapper();
        qw.like(map.get("key")!=null && !map.get("key").equals(""),"projectName",map.get("key"));
        qw.eq(map.get("status")!=null && !map.get("status").equals(""),"status",map.get("status"));
        qw.eq("node","待审核");
        IPage iPage = jiaogaiDao.selectPage(page, qw);
        return iPage;
    }

    /**
     * @Description 通过教改项目id查询详细信息
     * @Param [id]
     * @return cn.edu.gsau.info.models.Jiaogai
     * @Date 2022/11/21 17:04
     * @Author kql
     **/
    @Override
    public Jiaogai searchbyjiaogaiid(String id) {
        Jiaogai list = jiaogaiDao.selectById(id);
        return list;
    }

    /**
     * @Description //TODO
     * @Param [user, form]
     * @return int
     * @Date 2022/11/26 16:03
     * @Author kql
     **/
    @Override
    @Transactional
    public int add(User user, Jiaogai form) {
        Review review = reviewService.selectByModuleId(ModuleIdClass.JIAOGAI_id);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("id",review.getUserId());
        User user1 = userService.getOne(qw);

        Date date1 = new Date();
        form.setCreateBy(user.getNickName());
        form.setTianbaoId(user.getId());
        form.setTianbaoName(user.getNickName());
        form.setCreateTime(date1);
        form.setStatus("进行中");
        form.setNode("待审核");
        form.setManager(user1.getNickName());
        jiaogaiDao.insert(form);


            Date date2 = new Date();
            Shenhe shenHe = new Shenhe();
            shenHe.setModuleId(ModuleIdClass.JIAOGAI_id);
            shenHe.setRecoreId(form.getId());
            shenHe.setTijiaoId(form.getTianbaoId());
            shenHe.setTijiaoName(form.getCreateBy());
            shenHe.setTijiaoTime(date2);
            shenHe.setTijiaoMemo("请审核");
            shenHe.setShenheId(user1.getId());
            shenHe.setShenHeName(user1.getNickName());
            shenHeService.save(shenHe);

        FileName[] fileList = form.getFileList();
            for (int i = 0; i < fileList.length; i++) {
                Zhengmingcailiao zhengming = new Zhengmingcailiao();
                zhengming.setModuleId(ModuleIdClass.JIAOGAI_id);
                zhengming.setRecoreId(form.getId());
                zhengming.setFileName(fileList[i].getName());
                zhengming.setFileFullPath(user.getUserName() + "/" + fileList[i].getNewfilename());
                zhengMingCaiLiaoService.save(zhengming);
            }
            return 1;
    }
    /**
     * @Description
     * @Param [form]
     * @return java.lang.String
     * @Date 2022/11/26 16:03
     * @Author kql
     **/
    @Override
    @Transactional
    public int edit(Jiaogai form) {
        Review review = reviewService.selectByModuleId(ModuleIdClass.JIAOGAI_id);
        System.out.println(form.toString());
        QueryWrapper qw = new QueryWrapper();
        qw.eq("id",review.getUserId());
        User user1 = userService.getOne(qw);
        Jiaogai jiaogai = jiaogaiDao.selectById(form.getId());
        if (jiaogai.getNode().equals("审核未通过")) {
            Date date = new Date();
            form.setUpdateTime(date);
            form.setUpdateBy(form.getCreateBy());
            form.setNode("待审核");
            jiaogaiDao.updateById(form);

            Date date2 = new Date();
            Shenhe shenHe = new Shenhe();
            shenHe.setModuleId(ModuleIdClass.JIAOGAI_id);
            shenHe.setRecoreId(form.getId());
            shenHe.setTijiaoId(form.getTianbaoId());
            shenHe.setTijiaoName(form.getCreateBy());
            shenHe.setTijiaoTime(date2);
            shenHe.setTijiaoMemo("请审核");
            shenHe.setShenheId(user1.getId());
            shenHe.setShenHeName(user1.getNickName());
            shenHeService.save(shenHe);

            QueryWrapper qw1 = new QueryWrapper();
            qw1.eq("moduleId", ModuleIdClass.JIAOGAI_id);
            qw1.eq("recoreId", form.getId());
            zhengMingCaiLiaoService.remove(qw1);
            FileName[] fileList = form.getFileList();
            int insert = 0;
            for (int i = 0; i < fileList.length; i++) {
                Zhengmingcailiao zhengming = new Zhengmingcailiao();
                zhengming.setModuleId(ModuleIdClass.JIAOGAI_id);
                zhengming.setRecoreId(form.getId());
                zhengming.setFileName(fileList[i].getName());
                zhengming.setFileFullPath(form.getTianbaoName() + "/" + fileList[i].getNewfilename());
                zhengMingCaiLiaoService.save(zhengming);
            }
        }
        return  1;
    }
    /**
     * @Description //TODO
     * @Param [jiaogai]
     * @return int
     * @Date 2022/11/29 20:01
     * @Author kql
     **/
    @Override
    @Transactional
    public int shenPi(Jiaogai jiaogai) {
        System.out.println(jiaogai.toString());
        Jiaogai jiaogai1 = jiaogaiDao.selectById(jiaogai.getId());
        String shenHeStatus = jiaogai.getShenHeStatus();
        String shenHeYiJian = jiaogai.getShenHeYiJian();
        Date date = new Date();
        QueryWrapper qw = new QueryWrapper();
        qw.eq("recoreId",jiaogai.getId());
        qw.eq("moduleId",ModuleIdClass.JIAOGAI_id);
        qw.eq("memo",0);
        Shenhe one = shenHeService.getOne(qw);
        one.setShenHeTime(date);
        one.setShenHeResult(shenHeYiJian);
        one.setMemo("1");
        shenHeService.updateById(one);
        if (jiaogai.getShenHeStatus().equals("通过")){
            jiaogai1.setNode("审核已通过");
            jiaogai1.setStatus("流转完成");
        }else {
            jiaogai1.setNode("审核未通过");
        }
        int insert = jiaogaiDao.updateById(jiaogai1);
        return insert;
    }
    /**
     * @Description //TODO
     * @Param [id]
     * @return java.util.List<cn.edu.gsau.info.models.ShenHe>
     * @Date 2022/11/29 20:00
     * @Author kql
     **/
    @Override
    public List<Shenhe> shenheluicheng(long id) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("recoreId",id);
        qw.eq("moduleId",ModuleIdClass.JIAOGAI_id);
        List list = shenHeService.list(qw);
        return list;
    }
}

